import { _decorator, Component } from 'cc';
import { FogGrid } from './FogGrid';

const { ccclass, property } = _decorator;

@ccclass('VisibilityChecker')
export class VisibilityChecker extends Component {
    @property(FogGrid)
    private fogGrid: FogGrid | null = null;

    private wasVisible: boolean = false;

    update() {
        if (!this.fogGrid) return;

        const isVisible = this.fogGrid.isCellVisible(this.node.position);
        if (isVisible !== this.wasVisible) {
            this.wasVisible = isVisible;
            this.updateVisibility(isVisible);
        }
    }

    private updateVisibility(visible: boolean) {
        // 更新渲染组件的可见性
        const renderers = this.getComponentsInChildren('MeshRenderer');
        renderers.forEach(renderer => {
            renderer.enabled = visible;
        });

        // 更新UI组件的可见性
        const uiComponents = this.getComponentsInChildren('UIComponent');
        uiComponents.forEach(ui => {
            ui.node.active = visible;
        });
    }
} 